﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jalal.Library.Data;
using System.Data;
using System.Data.Common;
using Jalal.Library.Utility;

namespace BCGC.DAL
{
    public class ContactData
    {
        public static int UpdateContactData(int contactId, string clientName, string clientOrganization,
            string clientEmail, string clientPhone, string clientWebsite, string contactSubject, 
            string contactDescription, DateTime contactDateTime, bool isRead)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdateContact";
            dataMiner.AddParameter(command, "@ContactId", DbType.Int32, 0, ParameterDirection.Input, contactId);
            dataMiner.AddParameter(command, "@ClientName", DbType.String, 500, ParameterDirection.Input, clientName);
            dataMiner.AddParameter(command, "@ClientOrganization", DbType.String, 500, ParameterDirection.Input, clientOrganization);
            dataMiner.AddParameter(command, "@ClientEmail", DbType.String, 100, ParameterDirection.Input, clientEmail);
            dataMiner.AddParameter(command, "@ClientPhone", DbType.String, 50, ParameterDirection.Input, clientPhone);
            dataMiner.AddParameter(command, "@ClientWebsite", DbType.String, 500, ParameterDirection.Input, clientWebsite);
            dataMiner.AddParameter(command, "@ContactDescription", DbType.String, -1, ParameterDirection.Input, contactDescription);
            dataMiner.AddParameter(command, "@ContactSubject", DbType.String, 500, ParameterDirection.Input, contactSubject);
            dataMiner.AddParameter(command, "@ContactDateTime", DbType.DateTime, 0, ParameterDirection.Input, contactDateTime);
            dataMiner.AddParameter(command, "@IsRead", DbType.Boolean, 0, ParameterDirection.Input, isRead);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static int CreateNewContact(string clientName, string clientOrganization, 
            string clientEmail, string clientPhone, string clientWebsite, string contactSubject, 
            string contactDescription, DateTime contactDateTime, bool isRead)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CreateNewContact";
            dataMiner.AddParameter(command, "@ClientName", DbType.String, 500, ParameterDirection.Input, clientName);
            dataMiner.AddParameter(command, "@ClientOrganization", DbType.String, 500, ParameterDirection.Input, clientOrganization);
            dataMiner.AddParameter(command, "@ClientEmail", DbType.String, 100, ParameterDirection.Input, clientEmail);
            dataMiner.AddParameter(command, "@ClientPhone", DbType.String, 50, ParameterDirection.Input, clientPhone);
            dataMiner.AddParameter(command, "@ClientWebsite", DbType.String, 500, ParameterDirection.Input, clientWebsite);
            dataMiner.AddParameter(command, "@ContactDescription", DbType.String, -1, ParameterDirection.Input, contactDescription);
            dataMiner.AddParameter(command, "@ContactSubject", DbType.String, 500, ParameterDirection.Input, contactSubject);
            dataMiner.AddParameter(command, "@ContactDateTime", DbType.DateTime, 0, ParameterDirection.Input, contactDateTime);
            dataMiner.AddParameter(command, "@IsRead", DbType.Boolean, 0, ParameterDirection.Input, isRead);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (int)CommonUtility.FilterNull(id, typeof(int));
        }

        public static DataTable GetAllContacts(int currentPageIndex, string sortOrder, int pageSize, string searchName, 
            string searchOrganization, DateTime? searchContactDateFrom, DateTime? searchContactDateTo, 
            bool? searchRead, out int totalData)
        {
            totalData = 0;
            object count = null;

            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();

            dataMiner.AddParameter(command, "@CurrentPageIndex", DbType.Int32, 0, ParameterDirection.Input, currentPageIndex);
            dataMiner.AddParameter(command, "@SortOrder", DbType.String, 500, ParameterDirection.Input, sortOrder);
            dataMiner.AddParameter(command, "@PageSize", DbType.Int32, 0, ParameterDirection.Input, pageSize);
            dataMiner.AddParameter(command, "@SearchName", DbType.String, 500, ParameterDirection.Input, searchName);
            dataMiner.AddParameter(command, "@SearchOrganization", DbType.String, 500, ParameterDirection.Input, searchOrganization);
            dataMiner.AddParameter(command, "@SearchContactDateFrom", DbType.DateTime, 0, ParameterDirection.Input, searchContactDateFrom);
            dataMiner.AddParameter(command, "@SearchContactDateTo", DbType.DateTime, 0, ParameterDirection.Input, searchContactDateTo);
            dataMiner.AddParameter(command, "@SearchRead", DbType.Boolean, 0, ParameterDirection.Input, searchRead);

            command.CommandText = "CSP_GetTotalContactCount";
            count = dataMiner.GetOneData(command);
            totalData = (int)CommonUtility.FilterNull(count, typeof(int));

            command.CommandText = "CSP_GetContactList";
            return dataMiner.GetListData(command);
        }

        public static DataTable GetContactById(int contactId)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetContactById";
            dataMiner.AddParameter(command, "@ContactId", DbType.Int32, 0, ParameterDirection.Input, contactId);
            return dataMiner.GetListData(command);
        }

        public static int DeleteContacts(List<int> contactItemIds)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_DeleteContactItems";
            dataMiner.AddParameter(command, "@IdsToDelete", DbType.String, -1, ParameterDirection.Input, DataMiner.FormatXMLForIdArray(contactItemIds));
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static int UpdateReadItems(List<int> contactItemIds)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdateReadItems";
            dataMiner.AddParameter(command, "@IdsToMarkRead", DbType.String, -1, ParameterDirection.Input, DataMiner.FormatXMLForIdArray(contactItemIds));
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static int UpdateUnreadItems(List<int> contactItemIds)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdateUnreadItems";
            dataMiner.AddParameter(command, "@IdsToMarkUnread", DbType.String, -1, ParameterDirection.Input, DataMiner.FormatXMLForIdArray(contactItemIds));
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }
    }
}
